//
//  ESIMVC.swift
//  SDKDebuggingDemo
//
//  Created by 秦晟哲 on 2019/9/6.
//  Copyright © 2019 QinszQSZ. All rights reserved.
//

import UIKit

class ESIMVC: UIViewController {
    
    @IBOutlet weak var msgTextView: UITextView!
    
    @IBOutlet weak var rspTextView: UITextView!
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    @IBAction func clickBack(_ sender: Any) {
        self.navigationController?.popViewController(animated: true)
    }
    
    @IBAction func clickStart(_ sender: Any) {
        rspTextView.text = nil
        SVProgressHUD.show()
        let task = KCTOldBLETaskESIMStart.init {[weak self] (tsk,error) -> (Void) in
            SVProgressHUD.dismiss()
            self?.rspTextView.text = ResultContent(title: "Start", error: error, rspDes: nil)
        }
        KCTSingleBLEShell.sharedInstance.resume(baseTask: task)
    }
    @IBAction func clickEnd(_ sender: Any) {
        rspTextView.text = nil
        SVProgressHUD.show()
        let task = KCTOldBLETaskESIMEnd.init {[weak self] (tsk,error) -> (Void) in
            SVProgressHUD.dismiss()
            self?.rspTextView.text = ResultContent(title: "End", error: error, rspDes: nil)
        }
        KCTSingleBLEShell.sharedInstance.resume(baseTask: task)
    }
    
    
    @IBAction func clickStartESIMService(_ sender: Any) {
        rspTextView.text = nil
        SVProgressHUD.show()
        let task = KCTOldBLETaskESIMStartService.init {[weak self] (tsk, result, error) -> (Void) in
            SVProgressHUD.dismiss()
            self?.rspTextView.text = ResultContent(title: "StartService", error: error, rspDes: "result=\(result)")
        }
        KCTSingleBLEShell.sharedInstance.resume(baseTask: task)
    }
    @IBAction func clickEndESIMService(_ sender: Any) {
        rspTextView.text = nil
        SVProgressHUD.show()
        let task = KCTOldBLETaskESIMEndService.init {[weak self] (tsk,error) -> (Void) in
            SVProgressHUD.dismiss()
            self?.rspTextView.text = ResultContent(title: "EndService", error: error, rspDes: nil)
        }
        KCTSingleBLEShell.sharedInstance.resume(baseTask: task)
    }

    
    @IBAction func clickGetEID(_ sender: Any) {
        
        rspTextView.text = nil
        SVProgressHUD.show()
        let task = KCTOldBLETaskESIMGetDeviceEID.init {[weak self] (tsk, rawData, error) -> (Void) in
            SVProgressHUD.dismiss()
            if let data = rawData {
                self?.rspTextView.text = ResultContent(title: "GetDeviceEID", error: error, rspDes: "\(NSData.init(data: data))")
            }else{
                self?.rspTextView.text = ResultContent(title: "GetDeviceEID", error: error, rspDes: "空")
            }
        }
        KCTSingleBLEShell.sharedInstance.resume(baseTask: task)

    }
    @IBAction func clickGetIMEI(_ sender: Any) {
        rspTextView.text = nil
        SVProgressHUD.show()
        let task = KCTOldBLETaskESIMGetDeviceIMEI.init {[weak self] (tsk, rawData, error) -> (Void) in
            SVProgressHUD.dismiss()
            if let data = rawData {
                self?.rspTextView.text = ResultContent(title: "GetDeviceIMEI", error: error, rspDes: "\(NSData.init(data: data))")
            }else{
                self?.rspTextView.text = ResultContent(title: "GetDeviceIMEI", error: error, rspDes: "空")
            }
        }
        KCTSingleBLEShell.sharedInstance.resume(baseTask: task)
    }
    
    @IBAction func clickSendMsg(_ sender: Any) {
        
        rspTextView.text = nil
        
        if let text = msgTextView.text, let d = HexToData(hexStr: text) {
            SVProgressHUD.show()
            let task = KCTOldBLETaskESIMSendMsgToDevice.init(msgData: d) {[weak self] (tsk, rawData, error) -> (Void) in
                SVProgressHUD.dismiss()
                if let `rawData` = rawData {
                    self?.rspTextView.text = ResultContent(title: "GetDeviceIMEI", error: error, rspDes: "\(NSData.init(data: rawData))")
                }else{
                    self?.rspTextView.text = ResultContent(title: "GetDeviceIMEI", error: error, rspDes: "空")
                }
            }
            KCTSingleBLEShell.sharedInstance.resume(baseTask: task)
        }else{
            SVProgressHUD.showInfo(withStatus: "输入有误")
        }
    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.view.endEditing(true)
    }
    
    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
